﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Wpf.CommonTools.XML
{
    public class ConfigContent
    {
        private Dictionary<string, string> configInfos = null;
        private XmlTextWriter writer;
        private XmlTextReader reader;
        private String savePath;
        /// <summary>
        /// 存储路径
        /// </summary>
        public String SavePath
        {
            get { return savePath; }
            set { savePath = value; }
        }

        public string this[string key]
        {
            get
            {
                if (configInfos.ContainsKey(key))
                {
                    return configInfos[key];
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                configInfos[key] = value;
            }
        }
        /// <summary>
        /// XML操作
        /// </summary>
        public ConfigContent(string savePath)
        {
            this.SavePath = savePath;
            configInfos = new Dictionary<string, string>();
        }
        //public ConfigContent(string savePath, Dictionary<string,string> configs)
        //    : this(savePath)
        //{
        //    this.ConfigInfos = configs;
        //}


        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            try
            {
                writer = new XmlTextWriter(SavePath, System.Text.Encoding.UTF8);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            // Indent the XML document for readability
            writer.Formatting = System.Xml.Formatting.Indented;

            // Call WriteStartDocument to write xml declaration
            // should be the first call before writing elements
            writer.WriteStartDocument();

            // write a comment
            writer.WriteComment("Config xml");

            // write root element - should atleast contain a root element
            writer.WriteStartElement("HUGEJILE");

            WriteHeader();
            WriteContents();

            // Write End of root element
            writer.WriteEndElement();

            // Write End of document
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        private void WriteHeader()
        {
            writer.WriteStartElement("Header");
            writer.WriteAttributeString("CompanyName", "HUGEJILE.CN");
            System.Reflection.Assembly oAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            System.Diagnostics.FileVersionInfo oFileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(oAssembly.Location);
            writer.WriteElementString("Version", oFileVersionInfo.ProductVersion.ToString());
            writer.WriteElementString("Files", oFileVersionInfo.FileName.ToString());
            writer.WriteElementString("Author", "呼格吉勒");
            writer.WriteElementString("Email", "Hugejile@msn.com");
            writer.WriteElementString("CreatDate", System.DateTime.Today.ToShortDateString().ToString());
            writer.WriteElementString("CreatTime", System.DateTime.Now.TimeOfDay.ToString());
            writer.WriteEndElement();
        }

        private void WriteContents()
        {

            // Write element <Contents>
            writer.WriteStartElement("UserInfo");
            writer.WriteElementString("Username", "Hugejile.com");
            writer.WriteEndElement();
            writer.WriteStartElement("Config");
            foreach (KeyValuePair<string,string> item in configInfos)
            {
                writer.WriteElementString(item.Key, item.Value);
            }

            writer.WriteEndElement();
        }
        /// <summary>
        /// 读取
        /// </summary>
        public void ReadXmlDocument()
        {
            reader = new XmlTextReader(SavePath);
            if (configInfos.Count != 0)
            {
                try
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            configInfos[reader.LocalName] = reader.ReadElementContentAsString();
                        }
                    }
                }
                catch { }
                finally
                {
                    reader.Close();
                }
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(SavePath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                XmlNode root = doc.DocumentElement;
                foreach (XmlNode n in root.ChildNodes)
                {
                    if (n.Name == "Config")
                    {
                        foreach (XmlNode l in n.ChildNodes)
                        {
                            configInfos[l.Name] = l.InnerText;
                        }
                    }
                }
            }
        }
    }
}
